package com.assignwork.service.impl;

import com.assignwork.dto.DailySafeInfoSearchDTO;
import com.assignwork.dto.DailySafeInfoSubmitDTO;
import com.assignwork.dto.WorkArrangeFixedMachineSubmitDTO;
import com.assignwork.entity.DailySafeInfo;
import com.assignwork.entity.WorkArrangeFixedMachine;
import com.assignwork.mapper.DailySafeInfoMapper;
import com.assignwork.service.IDailySafeInfoService;
import com.assignwork.vo.DailySafeInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DailySafeInfoServiceImpl
 * @Description 每日安全告知接口实现
 * @Author dujianyu
 * @Date 2023/12/24 17:28
 */
@Service
public class DailySafeInfoServiceImpl extends BaseServiceImpl<DailySafeInfoMapper, DailySafeInfo> implements IDailySafeInfoService {
    @Override
    public boolean submitSafeInfo(DailySafeInfoSubmitDTO dto) {
        boolean removeOld = removeOldSafeInfo(dto.getInfoDate());
        if (!Func.isNull(dto.getSafeInfo()) && !Func.isEmpty(dto.getSafeInfo())) {
            List<DailySafeInfo> dailySafeInfos = new ArrayList<>();
            for (DailySafeInfoVO item : dto.getSafeInfo()) {
                DailySafeInfo newDailyInfo = new DailySafeInfo();
                newDailyInfo.setInfoDate(item.getInfoDate());
                newDailyInfo.setSafeInfo(item.getSafeInfo());
                dailySafeInfos.add(newDailyInfo);
            }
            return removeOld && super.saveOrUpdateBatch(dailySafeInfos);
        } else {
            return removeOld;
        }
    }

    @Override
    public List<DailySafeInfoVO> safeInfoByDate(DailySafeInfoSearchDTO dto) {
        QueryWrapper<DailySafeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(DailySafeInfo::getInfoDate, DailySafeInfo::getSafeInfo)
                .eq(DailySafeInfo::getIsDeleted, 0)
                .eq(!Func.isNull(dto.getInfoDate()), DailySafeInfo::getInfoDate, dto.getInfoDate())
                .orderByAsc(DailySafeInfo::getCreateTime);
        List<DailySafeInfo> temp = baseMapper.selectList(queryWrapper);
        return BeanUtil.copy(temp, DailySafeInfoVO.class);
    }

    @Override
    public boolean importFromPrevious(DailySafeInfoSearchDTO dto) {
        LocalDate date = dto.getInfoDate();
        LocalDate yesterday = date.minusDays(1);

        DailySafeInfoSearchDTO yesterdaySearch = new DailySafeInfoSearchDTO();
        yesterdaySearch.setInfoDate(yesterday);
        List<DailySafeInfoVO> yesterdayInfos = safeInfoByDate(yesterdaySearch);
        if (Func.isEmpty(yesterdayInfos)) {
            throw new RuntimeException("前一日无安全告知");
        }

        List<DailySafeInfo> newInfoList = new ArrayList<>();
        for (DailySafeInfoVO yesterdayInfo : yesterdayInfos) {
            DailySafeInfo info = new DailySafeInfo();
            info.setSafeInfo(yesterdayInfo.getSafeInfo());
            info.setInfoDate(date);
            newInfoList.add(info);
        }

        return super.saveOrUpdateBatch(newInfoList);
    }

    private boolean removeOldSafeInfo(LocalDate date) {
        QueryWrapper<DailySafeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(DailySafeInfo::getId).eq(DailySafeInfo::getIsDeleted, 0)
                .eq(DailySafeInfo::getInfoDate, date);
        List<DailySafeInfo> infos = baseMapper.selectList(queryWrapper);
        if (!Func.isNull(infos) && !Func.isEmpty(infos)) {
            List<Long> ids = infos.stream().map(DailySafeInfo::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        } else {
            return true;
        }
    }
}
